<template>
    <div class="beijing">
        <div class="entirety">
            <div class="pic">
                <!-- <img src="../assets/image/login.png" alt="" /> -->
                <img src="../assets/image/icon-login.png" alt="" style="width: 350px; height: 350px;" />
            </div>
            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" :label-position="labelPosition" class="form">
                <h4>欢迎登录！</h4>
                <h4>冀网联数据交易服务平台</h4>
                <div class="title" id="wrap">
                    没有账号？去<router-link to="/ZhuCe" class="router">注册</router-link>
                </div>
                <el-form-item prop="phone" label-width="30px" class="item">

                    <el-input placeholder="请输入手机号" class="int" clearable v-model="ruleForm.phone">
                        <i slot="prefix" class="el-icon-phone-outline" style="font-size:18px;
              padding-right: 20px;
              color:rgb(169, 170, 170);
              "></i></el-input>
                </el-form-item>
                <el-form-item prop="password" label-width="30px">


                    <el-input placeholder="请输入验证码" class="int" clearable v-model="ruleForm.code">
                        <i slot="prefix" class="el-icon-thumb" style="font-size:18px;
              padding-right: 20px;
              color:rgb(169, 170, 170);
              "></i>
                        <template #suffix>
                            <el-button type="text" @click="sendCode" :disabled="ifDisable">
                                <span v-if="!ifDisable"> 获取验证码</span>
                                <span v-else type="text">{{ selfCount }}秒后重新发送</span>
                            </el-button>
                        </template>
                    </el-input>

                </el-form-item>
                <div class="but-box">
                    <el-button type="text" @click="ToLogin">切换至密码登录</el-button>
                </div>
                <el-form-item style="width: 100%; text-align: center; margin-top: 20px;">
                    <el-button type="primary" round @click="login(ruleForm)">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import { isPhoneNumber, YanzhangLogin, DengluYanzhang } from "../api/api.js";
export default {
    data() {
        return {
            ifDisable: false,
            selfCount: 60,
            labelPosition: "right",
            ruleForm: {},
            rules: {
                phone: [{ required: true, trigger: "blur", message: "请输入手机号" }],
                code: [{ required: true, trigger: "blur", message: "请输入验证码" }],
            },
        };
    },

    methods: {
        sendCode(formName) {
            //校验手机号
            if (isPhoneNumber(this.ruleForm.phone)) {
                this.$message({
                    message: '验证码已发送',
                    type: 'success'
                })
            } else {
                this.$message.error('请输入正确的手机号')
                return
            }
            //验证码倒计时
            this.ifDisable = true
            let timer = setInterval(() => {
                if (this.selfCount == 0) {
                    clearInterval(timer)
                    this.ifDisable = false
                    this.selfCount = 60
                }
                this.selfCount--
            }, 1000)
            //获取验证码
            DengluYanzhang(this.ruleForm.phone)
                .then((res) => {
                    console.log(res.data)
                })
                .catch((error) => {
                    console.log('验证码获取失败', error)
                })
        },

        login() {
            if (isPhoneNumber(this.ruleForm.phone)) {
            } else {
                this.$message.error("请输入正确的手机号");
                return;
            }
            YanzhangLogin(this.ruleForm.phone, this.ruleForm.code)
                .then((res) => {
                    if (res.data.code != 200) {
                        this.$message({
                            message: res.data.message,
                            type: "warning",
                        });
                    } else if (res.data.code == 200) {
                        localStorage.setItem("list", JSON.stringify(res.data.data))
                        this.$message({
                            message: '登录成功',
                            type: "success",
                        });
                        this.$router.push("/");
                        location.reload()
                    }
                })
                .catch((error) => {

                });
        },
        ToLogin() {
            this.$router.push('/Login')
        }
    }
}
//




</script>
<style lang="less" scoped>
.beijing {
    width: 100%;
    // height: calc(100% - 80px);
    height: 100%;
    background-image: url("../assets/image/bglogin.png");
    display: flex;
    justify-content: center;
    align-items: center;
    background-size: cover;

    .entirety {
        width: 850px;
        height: 600px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: aliceblue;
        border-radius: 15px;

        .pic {
            margin-right: 10px;
        }

        h4 {
            color: #000088;
            margin-bottom: 20px;
            margin-left: 30px;
        }

        .title {
            font-size: 14px;
            display: flex;
            justify-content: right;
            margin-top: 20px;
            margin-left: 98px;
            padding: 5px;


        }

        .form {
            width: 350px;
            padding-top: 40px;
        }

        .item {
            margin-top: 20px;
        }

        .router {
            text-decoration: none;
        }

        .int {
            width: 250px;

        }

        /deep/ .el-input__inner {
            border-top-width: 0px;
            border-left-width: 0px;
            border-right-width: 0px;
            border-bottom-width: 2px;
            background-color: transparent !important;
        }

        .but-box {
            width: 100%;
            display: flex;
            flex-direction: row;
            // justify-content: space-between;
            margin-left: 30px;

        }
    }
}

.yanzheng {
    width: 15%;
    padding: 5px;
}
</style>